package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;

import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: YKYB
 * @Date: 2025/9/17 18:02
 * @Description:
 */
@RestController
@RequestMapping("/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    @GetMapping("/page")
    public Result<PageInfo<Process>> list(
            @RequestParam(required = false) String processCode,
            @RequestParam(required = false) String processName,
            @RequestParam(required = false) Integer isActive,
            @RequestParam(defaultValue = "1")  int pageNum,
            @RequestParam(defaultValue = "10") int size) {

        Process query = new Process();
        query.setProcessCode(processCode);
        query.setProcessName(processName);
        query.setIsActive(isActive);   // 空串已转 null
        return processService.selectList(query, pageNum, size);
    }

    @PostMapping("/add")
    public Result<Process> add(@RequestBody Process process) {
        return processService.addProcess(process);
    }

    @GetMapping("/code/{code}")
    public Result<Process> getByCode(@PathVariable String code) {
        return processService.getByProcessCode(code);
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return processService.deleteById(id);
    }

    @DeleteMapping("/batchDelete")
    public Result<?> batchDelete(@RequestParam List<Long> id) {
        return processService.batchDeleteByIds(id);
    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody Process process) {
        return processService.updateById(process);
    }
}
